package com.yami.shop.admin.controller;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.dto.MallDto;
import com.yami.shop.bean.model.Mall;
import com.yami.shop.bean.model.ProdTag;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.MallService;
import com.yami.shop.service.ProdTagService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/prod/mall")
public class MallController {
    @Autowired
    private MallService mallService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param
     * @return 分页数据
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<MallDto>> getMallPage(PageParam<MallDto> page, Mall mall) {

	    Map<String, Object> params = new HashMap<>();
	    params.put("mallName",mall.getMallName());


	    IPage<MallDto> iPage =mallService.query(page,params);
	    return ServerResponseEntity.success(iPage);

    }


    /**
     * 通过id查询商品分组标签
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    public ServerResponseEntity<MallDto> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(mallService.getMallById(id));
    }

    /**
     * 新增商城
     *
     * @  商城
     * @return 是否新增成功
     */
    @SysLog("新增商城")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('prod:mall:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid Mall mall) {
        // 查看是否相同的标签
        List<Mall> list = mallService.list(new LambdaQueryWrapper<Mall>().like(Mall::getMallName, mall.getMallName()));
        if (CollectionUtil.isNotEmpty(list)) {
            throw new YamiShopBindException("商城名称已存在，不能添加相同的商城名称");
        }

        return ServerResponseEntity.success(mallService.save(mall));
    }

    /**
     * 修改商城
     *
     * @  商城
     * @return 是否修改成功
     */
    @SysLog("修改商城")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('prod:mall:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid Mall mall) {

        return ServerResponseEntity.success(mallService.updateById(mall));
    }

    /**
     * 通过id删除商城
     *
     * @param ids
     * @return 是否删除成功
     */
    @SysLog("删除商城")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('prod:mall:delete')")
    public ServerResponseEntity<Boolean> removeById(@RequestBody Long[] ids) {
        for (Long id : ids) {
            mallService.removeById(id);
        }
        return ServerResponseEntity.success();

    }

    @GetMapping("/listMallList")
    public ServerResponseEntity<List<Mall>> listMallList() {
        List<Mall> list=mallService.list();
        Mall mall=new Mall();
        mall.setId(0);
        mall.setMallName("全部商城");
        list.add(0,mall);
        return ServerResponseEntity.success(list);

    }
}
